# encoding: utf-8
from pyquery import PyQuery as pq
import re
import string
import sys
import os
import types
import urllib2  
import json
reload(sys)
sys.setdefaultencoding("utf8");

#全局数组，用来做排名使用
_array=[]

#取得单个商品赠送的陆金币,由于是ajax返回的，所以起一个方法单独处理
def getCoin(url):  
    try:  
        data = urllib2.urlopen(url).read() 
        _data = json.loads(data)
        coin = _data["coinDetaiDataGsons"][0]["coinCurrency"]
        return coin 
    except Exception,e: 
        print e

#处理单个商品返回的价格，通币，链接，图片等信息
def detail(_url):
    _doc=pq(url=_url)
    _data=_doc("#productCode").val()
    ajaxUrl = "https://lumi.lu.com/lumi/service/lumi/coupon-order/get-coupon-by-product-code?productCode="+_data 
    _coin=getCoin(ajaxUrl)
    
    _data=_doc(".summary-data .price span").eq(0)
    _lumicoin=_data.text()
    _lumicoin=_lumicoin.replace(",","")
    _lumicoin=string.atoi(_lumicoin)

    _data=_doc(".summary-data .price span").eq(3)
    _price=_data.text()
    _price=_price.replace(",","")
    if _price!="":
        _price=string.atof(_price)
    else:
        _price = 100000.0
    _data=_doc(".product-name").eq(0)
    _productName=_data.text()
    _picture=_doc(".img-product-large").attr("src")
    _picture="https:"+_picture
    _picture=_picture.replace("-big.jpg","-small.jpg")
    _obj = {}
    _obj["_productName"] = _productName
    _obj["_lumicoin"] = _lumicoin
    _url = _url.replace("exchange/product","exchange/product-mobile")
    _obj["_url"] = _url
    _obj["_coin"] = _coin
    _obj["_price"] = _price
    _obj["_picture"] = _picture
    if type(_coin) is not types.IntType:
        _coin = 1     
    _obj["_diff"] = _price/_coin
    _obj["_sale"] = str(round(_coin/_price*100,2))+"%"
    _array.append(_obj)

# 通过单个商品的返回属性生成单个商品的html片段
def getProductHtml(obj):
    _lihtml = ""
    _lihtml +=  "       <div class='media'>\n"
    _lihtml +=  "           <div class='media-left media-middle'>\n"
    _lihtml +=  "               <a href='$url' target=_blank>\n" 
    _lihtml +=  "                   <img width=160 class='media-object' src='$picture'>\n"
    _lihtml +=  "               </a>\n"
    _lihtml +=  "           </div>\n"
    _lihtml +=  "           <div class='media-body'>\n"
    _lihtml +=  "               <p class='media-heading'><strong>$productName</strong></p>\n" 
    _lihtml +=  "               <p>价格:$price</p>\n" 
    _lihtml +=  "               <p>送陆金币:$coin</p>\n" 
    _lihtml +=  "               <p>陆金所为您节省了<strong>$sale</strong>\n" 
    _lihtml +=  "               <p>需要通币:$lumi</p>\n" 
    _lihtml +=  "               <a href='$url' class='btn btn-default' target=_blank>购买</a>\n" 
    _lihtml +=  "           </div>\n"
    _lihtml +=  "       </div>\n"
    _lihtml = _lihtml.replace("$lumi", str(obj["_lumicoin"]))
    _lihtml = _lihtml.replace("$url", obj["_url"])
    _lihtml = _lihtml.replace("$productName", obj["_productName"])
    _lihtml = _lihtml.replace("$price",str(obj["_price"]))
    _lihtml = _lihtml.replace("$picture",str(obj["_picture"]))
    _lihtml = _lihtml.replace("$coin",str(obj["_coin"]))
    _lihtml = _lihtml.replace("$sale",str(obj["_sale"]))
    return _lihtml 

#配合template.html，生成线上的html页面
def setHtml(top, title, fileName, img):
    _html = ""
    _array.sort(key = lambda x:x['_diff'], cmp=lambda x,y: cmp(float(x), float(y)))
    if  top > len(_array):
        top = len(_array)
    for i in range(0,top):
        obj=_array[i]
        _html += getProductHtml(obj) 
    file_object=open("template.html","r+w")
    new_file_object=open("top_"+fileName+".html","w+r") 
    allhtml=""
    try:
        allhtml=file_object.read()
        allhtml=allhtml.replace("${content}",_html)
        allhtml=allhtml.replace("${title}",title)
        allhtml=allhtml.replace("${img}",img)
        new_file_object.truncate()
        new_file_object.write(allhtml)
    finally:
        file_object.close();
        new_file_object.close();
    

#全局数组，配置array，生成html页面前的配置准备。
#category: 抓取类型
#categoryName: 类型名称，在title展示用
#fileName: 生成的文件名称
#top: 排名的个数
#img: 用于微信分享的图片
infoArray=[]

def setInfoArray(category, categoryName, fileName, top, img):
    if img=="":
        img="https://s2-img1.lufaxcdn.com/product_image/PD150911009472/PD150911009472-big.jpg"
    obj={}
    obj["category"] = category
    obj["categoryName"] = categoryName
    obj["fileName"] = fileName
    obj["top"] = top
    obj["img"] = img
    infoArray.append(obj)
#setInfoArray("10,9,71,67,1,6,65,38,46,52,8", "", "all", 50,"")
#setInfoArray("7", "家居百货", "jiajubaihuo", 20,"https://s2-img2.lufaxcdn.com/product_image/PD150917009897/PD150917009897-big.jpg")
#setInfoArray("8", "全球美食", "meishi", 10,"https://s2-img1.lufaxcdn.com/product_image/PD150917009900/PD150917009900-big.jpg")
setInfoArray("46", "时尚配饰", "shishang", 20,"https://s2-img1.lufaxcdn.com/product_image/PD150915009694/PD150915009694-big.jpg")
#setInfoArray("9,71,67", "数码", "shuma", 10)


#抓包的主方法
def doDetects():
    for i in range(0,len(infoArray)):
        category=infoArray[i]["category"]
        categoryName = infoArray[i]["categoryName"]
        fileName = infoArray[i]["fileName"]
        top = infoArray[i]["top"]
        img = infoArray[i]["img"]
        title = "陆金所"+categoryName+"商品最划算top"+str(top)
        category="&category="+category.replace(",","&category=")
        url = "https://lumi.lu.com/lumi/filter?sortType=6&tradeType=EXCHANGE"+category+"&page=1" 
        page=getPageCount(url) 
        print(page) 
        for j in range(1,int(page)+1):
            _url = url.replace("&page=1","&page="+str(j))
            detectList(_url)
        setHtml(top, title, fileName,img)
        
#抓每一页的商品列表
def detectList(url):
    doc=pq(url=url)
    data=doc('.product-img a')
    for i in data:
        _url=pq(i).attr("href")
        detail(_url)



#取得每种类型商品的分页数
def getPageCount(url):
    _doc=pq(url=url)
    return _doc(".total-page").text()


doDetects()

#部署到新浪云
os.system("svn add *")
os.system("svn commit -m 'fx'")
